import dayjs from "dayjs";
import { Ref, computed } from "vue";
// import CommonHook from "@/hook/commonHook";
import { message as Message } from "ant-design-vue";
import { getListApi, getPageStaticApi } from "@/api/waybillManagement/businessWaybill/detainDoods";
export default function OperationHook(state: {
  searchForm: DetainGoods.SearchForm;
  tableData: Ref<Array<DetainGoods.ListRows>>;
  drawerOpts: Base.DrawerOpts;
  dialogOpts: Base.DialogOpts;
  loading: Ref<boolean>;
  tableRef: any;
  pageStat: Ref<Base.GeneraDataType>;
  activeKey: Ref<string>;
}) {
  const { searchForm, tableData, loading, tableRef, dialogOpts, pageStat, activeKey } = state;
  const selectRows = computed<Array<DetainGoods.ListRows>>(() => {
    return tableRef.value.xTable.getCheckboxRecords() || [];
  });
  // const { getNoZeroKey } = CommonHook();
  const getData = async () => {
    const params = {
      ...getParams(),
      releaseStatusList: []
    };
    if (activeKey.value === "-1") params.releaseStatusList = [1, 2, 3, 4, 5];
    else if (activeKey.value === "4") params.releaseStatusList = [4, 5];
    else params.releaseStatusList = [Number(activeKey.value)];
    loading.value = true;
    return getListApi(params)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data.records || [];
          searchForm.total = Number(data.total);
          searchForm.beforePageNum = searchForm.pageNum;
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  const pageStateic = () => {
    const params = getParams();
    return getPageStaticApi({ ...params, releaseStatusList: [1, 2, 3, 4, 5] }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        Object.assign(pageStat.value, {
          1: data[1],
          2: data[2],
          3: data[3],
          4: data[4],
          "-1": data[-1]
        });
        // activeKey.value = String(getNoZeroKey(pageStat.value, ["1", "2", "3", "4", "-1"]));
      }
    });
  };
  const getParams = () => {
    const {
      releaseReasonList,
      detentionTime,
      waybillNums,
      bizIdList,
      beforePageNum,
      customerIdList,
      companyIdList,
      followIdList,
      channelIdList,
      countryRegionIdList,
      depositTime,
      createTime,
      pageNum,
      pageSize
    } = searchForm;
    const params: any = {
      waybillNums: [],
      releaseStatusList: [],
      companyIdList,
      customerIdList,
      bizIdList,
      followIdList,
      channelIdList,
      countryRegionIdList,
      createStartTime: createTime.length ? dayjs(createTime[0]).startOf("day").format("YYYY-MM-DD HH:mm:ss") : undefined,
      createEndTime: createTime.length ? dayjs(createTime[1]).endOf("day").format("YYYY-MM-DD HH:mm:ss") : undefined,
      depositStartTime: depositTime.length ? dayjs(depositTime[0]).startOf("day").format("YYYY-MM-DD HH:mm:ss") : undefined,
      depositEndTime: depositTime.length ? dayjs(depositTime[1]).endOf("day").format("YYYY-MM-DD HH:mm:ss") : undefined,
      detentionStartTime: detentionTime?.length ? dayjs(detentionTime[0]).format("YYYY-MM-DD HH:mm:ss") : undefined,
      detentionEndTime: detentionTime?.length ? dayjs(detentionTime[1]).format("YYYY-MM-DD HH:mm:ss") : undefined,
      releaseReasonList,
      pageNum,
      pageSize,
      beforePageNum
    };
    if (waybillNums)
      params.waybillNums = waybillNums
        .trim()
        .replace(/,{1,}|，{1,}|\+{1,}|\s{1,}/g, ",")
        .split(",");
    if (channelIdList.length) {
      params.channelIdList = channelIdList.map((t: any) => t[t.length - 1]);
    }
    return params;
  };
  const searchHandle = async () => {
    searchForm.pageNum = 1;
    await pageStateic();
    getData();
  };
  const changeTabHandle = () => {
    getData();
  };
  // 申请放货
  const applyReleaseCargoHandle = () => {
    if (!selectRows.value.length) return Message.error("请勾选运单!");
    const allCustomerId = selectRows.value.map((t: DetainGoods.ListRows) => t.customerId);
    if (Array.from(new Set(allCustomerId)).length > 1) return Message.error("只能勾选相同客户的运单数据!");
    dialogOpts.title = `申请放货`;
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = "ApplyReleaseCargo";
    dialogOpts.passData = selectRows.value;
    dialogOpts.visible = true;
  };

  return {
    searchHandle,
    getData,
    changeTabHandle,
    applyReleaseCargoHandle,
    pageStateic
  };
}
